package com.bjtg.threegoclinet.base;

import android.util.Xml;

import com.bjtg.threegoclinet.bean.GlobleMessage;
import com.bjtg.threegoclinet.bean.ParameterSet;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * 读写配置文件
 *
 * @author wdw
 * @
 */
public class PullParser {

    public static void parseParamSet(InputStream is) throws Exception {

        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is, "utf-8");

        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:

                    break;
                case XmlPullParser.START_TAG:
                    setParamSet(eventType, parser);
                    break;
                case XmlPullParser.END_TAG:

                    break;
            }
            eventType = parser.next();
        }
        is.close();
    }

    /**
     * 读取配置XML中，内容体的数据，将它们放入公共静态变量参数设置类中
     *
     * @param eventType
     * @param parser
     * @throws Exception
     */
    private static void setParamSet(int eventType, XmlPullParser parser) throws Exception {
        ParameterSet ps = GlobleMessage.getParamSet();

        if (parser.getName().equals("machineNo")) {
            eventType = parser.next();
            ps.setMachineNo(parser.getText());
        } else if (parser.getName().equals("rid")) {
            eventType = parser.next();
            ps.setRid(parser.getText());
        } else if (parser.getName().equals("machineStatus")) {
            eventType = parser.next();
            ps.setMachineStatus(parser.getText());
        } else if (parser.getName().equals("machineLayerNum")){
            eventType = parser.next();
            ps.setMachineLayerNum(parser.getText());
        }else if (parser.getName().equals("machineDoorType")){
            eventType = parser.next();
            ps.setMachineDoorType(parser.getText());
        }else if (parser.getName().equals("machineType")){
            eventType = parser.next();
            ps.setMachineType(parser.getText());
        }else if (parser.getName().equals("machineLineNum")){
            eventType = parser.next();
            ps.setMachineLineNum(parser.getText());
        }
    }

    public static void serializeParameSet(ParameterSet ps, OutputStream os) throws Exception {

        XmlSerializer serializer = Xml.newSerializer();

        serializer.setOutput(os, "UTF-8");
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", "parameterSet");

        serializer.startTag("", "machineNo");
        serializer.text(ps.getMachineNo());
        serializer.endTag("", "machineNo");

        serializer.startTag("", "rid");
        serializer.text(ps.getRid());
        serializer.endTag("", "rid");

        serializer.startTag("", "machineStatus");
        serializer.text(ps.getMachineStatus());
        serializer.endTag("", "machineStatus");

        serializer.startTag("", "machineLayerNum");
        serializer.text(ps.getMachineLayerNum());
        serializer.endTag("", "machineLayerNum");

        serializer.startTag("", "machineDoorType");
        serializer.text(ps.getMachineDoorType());
        serializer.endTag("", "machineDoorType");

        serializer.startTag("", "machineType");
        serializer.text(ps.getMachineType());
        serializer.endTag("", "machineType");

        serializer.startTag("", "machineLineNum");
        serializer.text(ps.getMachineLineNum());
        serializer.endTag("", "machineLineNum");

        serializer.endTag("", "parameterSet");
        serializer.endDocument();

        os.flush();
        os.close();

    }
}
